﻿using EquipmentManager.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EquipmentManager.BOLogic
{
    public class DirectorateBO : BOBase
    {
        public List<Directorate> GetDirectorateList()
        {
            try
            {
                var directorates = db.Set<Directorate>().Where(d => d.Status == EntityStatus.Active).OrderBy(d => d.Name).ToList();

                return directorates;
            }
            catch (Exception ex)
            {
                return new List<Directorate>();
            }
        }

        public Directorate GetDirectorate(long DirectorateId)
        {
            try
            {
                var directorate = db.Set<Directorate>().Single(d => d.Id == DirectorateId);

                return directorate;
            }
            catch (Exception ex)
            {
                return new Directorate();
            }
        }

        public bool CreateNewDirectorate(Directorate model)
        {
            try
            {
                var admin = db.Set<User>().Single(c => c.Id == User.Id);

                if (admin.Role == UserRole.Admin)
                {
                    model.Status = EntityStatus.Active;

                    db.Set<Directorate>().Add(model);
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool EditDirectorate(Directorate model)
        {
            try
            {
                var directorate = db.Set<Directorate>().Single(c => c.Id == model.Id);
                var admin = db.Set<User>().Single(c => c.Id == User.Id);

                if (admin.Role == UserRole.Admin)
                {
                    // можно создавать новую измененную, а эту делать неактивной если что;)
                    directorate.Name = model.Name;
                    directorate.Description = model.Description;
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeleteDirectorate(long DirectorateId)
        {
            try
            {
                var directorate = db.Set<Directorate>().Single(c => c.Id == DirectorateId);
                var admin = db.Set<User>().Single(c => c.Id == User.Id);

                if (admin.Role == UserRole.Admin)
                {
                    directorate.Status = EntityStatus.Deleted;
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}